package com.chexiehui.jc.ui.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;

import com.chexiehui.jc.AppApplication;
import com.chexiehui.jc.Constants;
import com.chexiehui.jc.R;
import com.chexiehui.jc.adapter.FragmentViewPagerAdapter;
import com.chexiehui.jc.base.BaseActivity;
import com.chexiehui.jc.ui.fragment.BrandListFragment;
import com.chexiehui.jc.ui.fragment.CarGarageAssistantFragment;
import com.chexiehui.jc.ui.fragment.CarGarageFragment;
import com.chexiehui.jc.ui.fragment.ForumFragment;
import com.chexiehui.jc.ui.fragment.HomeFragment;
import com.chexiehui.jc.ui.fragment.MineFragment;
import com.chexiehui.jc.ui.fragment.SpecialCarListFragment;
import com.chexiehui.jc.ui.fragment.TempFragment;
import com.chexiehui.jc.widget.NoTouchViewPager;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.PermissionListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.PageNavigationView;
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem;
import me.majiajie.pagerbottomtabstrip.item.NormalItemView;

public class MainActivity extends BaseActivity {

    @BindView(R.id.main_NoTouchViewPager)
    NoTouchViewPager mainNoTouchViewPager;
    @BindView(R.id.main_PageBottomTabLayout)
    PageNavigationView mainPageBottomTabLayout;

    private NavigationController navigationController;
    private List<Fragment> fragmentList = new ArrayList<>();

    @Override
    protected void setTitleBar() {

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView(Bundle var1) {

        fragmentList.add(HomeFragment.getInstance());
        fragmentList.add(ForumFragment.getInstance());
        fragmentList.add(CarGarageFragment.getInstance());
        //fragmentList.add(SpecialCarListFragment.getInstance());
        fragmentList.add(BrandListFragment.getInstance());
        fragmentList.add(MineFragment.getInstance());
        mainNoTouchViewPager.setOffscreenPageLimit(fragmentList.size());
        navigationController = mainPageBottomTabLayout.custom()
                .addItem(newItem(R.mipmap.ic_tab_home_us, R.mipmap.ic_tab_home_s, "首页"))
                .addItem(newItem(R.mipmap.iv_tab_forum_us, R.mipmap.iv_tab_forum_s, "论坛"))
                .addItem(newItem(R.mipmap.iv_tab_car_us, R.mipmap.iv_tab_car_s, "选车"))
                .addItem(newItem(R.mipmap.ic_tab_esj_us, R.mipmap.ic_tab_esj_s, "新车批发"))
                .addItem(newItem(R.mipmap.iv_tab_profile_us, R.mipmap.iv_tab_profile_s, "我的"))
                .build();
        mainNoTouchViewPager.setAdapter(new FragmentViewPagerAdapter(getSupportFragmentManager(), fragmentList));
        navigationController.setupWithViewPager(mainNoTouchViewPager);
        requestAppPermissions();
    }

    private void requestAppPermissions() {
        AndPermission.with(this)
                .permission(Permission.LOCATION, Permission.STORAGE)
                .callback(new PermissionListener() {
                    @Override
                    public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
                        AppApplication.getInstances().getLocationService().start();
                    }

                    @Override
                    public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
                        if (AndPermission.hasAlwaysDeniedPermission(mContext, deniedPermissions)) {
                            AndPermission.defaultSettingDialog(mContext, Constants.REQUEST_CODE_LOACTION).show();
                        } else {
                            showToast(getStringSource(R.string.app_name) + "需要您授权位置服务和访问文件的权限");
                        }
                    }
                })
                .requestCode(Constants.REQUEST_CODE_CARAME)
                .rationale((requestCode, rationale) -> AndPermission.rationaleDialog(mContext, rationale).show()).start();
    }

    @Override
    protected void onDestroy() {
        AppApplication.getInstances().getLocationService().stop();
        super.onDestroy();
    }

    @Override
    public boolean isActivityExitAppTips() {
        return true;
    }

    private BaseTabItem newItem(int drawable, int checkedDrawable, String text) {
        NormalItemView normalItemView = new NormalItemView(this);
        normalItemView.initialize(drawable, checkedDrawable, text);
        normalItemView.setTextDefaultColor(ContextCompat.getColor(mContext, R.color.c_333333));
        normalItemView.setTextCheckedColor(ContextCompat.getColor(mContext, R.color.app_color));
        return normalItemView;
    }
}
